/**
 * Created by CHENED2 on 8/26/2015.
 */

var casper = require('casper').create();
casper.start('http://www.baidu.com/', function() {
    this.echo(this.getTitle());
});
casper.run();

//var casper1 = require('casper').create();

//casper.start('http://casperjs.org/', function() {
//    this.echo(this.getTitle());
//});
//
//casper.thenOpen('http://phantomjs.org', function() {
//    this.echo(this.getTitle());
//});

//casper1.start('http://item.jd.com/1378538.html', function() {
//    this.echo(this.getTitle());
//});
//
//casper1.thenOpen('http://www.baidu.com', function() {
//    this.echo(this.getTitle());
//});
//
//casper1.run();


var casper = require('casper').create();
casper.start('http://google.fr/', function() {
    this.fill('form[action="/search"]', { q: 'casperjs' }, true);
});



var links = [];
var casper = require('casper').create();
console.log('aaa');
function getLinks() {
    var links = document.querySelectorAll('h3 a');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('href');
    });
}

casper.start('http://www.baidu.com', function() {
    this.echo('xxx');
    // search for 'casperjs' from google form
    this.fill('form[action="/s"]', { q: 'casperjs' }, true);
    var links = document.querySelectorAll('h3 a');
    console.log(links);
});

casper.then(function() {
    // aggregate results for the 'casperjs' search
    links = this.evaluate(getLinks);
    // now search for 'phantomjs' by filling the form again
   // this.fill('form[action="/search"]', { q: 'phantomjs' }, true);
});

casper.then(function() {
    // aggregate results for the 'phantomjs' search
    links = links.concat(this.evaluate(getLinks));
});

casper.run(function() {
    // echo results in some pretty fashion
    this.echo(links.length + ' links found:');
    this.echo(' - ' + links.join('\n - ')).exit();
});
